lane :beta do
    changelog = File.read('changelog.txt')
    last_tag = `git describe --tags --abbrev=0 --match builds/\*`.chomp
    testflight_changelog = changelog + "\n" + File.read("footer.txt") + `git shortlog #{last_tag}..HEAD`

    latest = latest_testflight_build_number.to_s.scan(/^\d+(?=\.|$)/).first.to_i
    build_number = latest + 1
    increment_build_number(build_number: build_number)
    commit_version_bump(
        xcodeproj: 'iSH.xcodeproj',
        message: "Release build #{build_number} to testflight",
        include: "fastlane/changelog.txt",
        force: true,
    )

    build_app(project: 'iSH.xcodeproj')
    upload_to_testflight(
        changelog: testflight_changelog,
        wait_for_uploaded_build: true,
        distribute_external: true,
        groups: ["People"]
    )

    tag = "builds/#{build_number}"
    add_git_tag(tag: tag)
    push_to_git_remote
    set_github_release(
        repository_name: 'ish-app/ish',
        tag_name: tag,
        commitish: null, # the tag better exist
        name: "Build #{build_number}",
        description: changelog,
        upload_assets: ['iSH.ipa'],
        api_token: ENV['GITHUB_TOKEN'],
    )
    slack(
        message: "New build available!",
        default_payloads: [],
        payload: {
            "Changelog" => changelog
        },
        use_webhook_configured_username_and_icon: true,
     )
end
